/*
 *  UI_FaderMatrixPanel.cpp
 *
 *	Date: 2011-4-27
 *  Author: linshenqi
 */

#include "stdafx.h"
#include "./UI_Frame.h"
#include <list>

extern std::list<GraphicBase *> g_GBList;

UI_FaderMatrixPanel::UI_FaderMatrixPanel(const CRect *pRect, const GRAPHICPROP *pProp)
	: GraphicBase(pRect, pProp)
{
	for(int i = 0; i < FMB_NUM; ++i)
	{
		m_pTB[i] = new UI_ToggleButton;
		m_pTB[i]->SetID(i);
		m_pTB[i]->SetText("0");
	}

	//m_pRotate = new UI_Rotate;
	//m_pRotate->SetID(0);
}

UI_FaderMatrixPanel::~UI_FaderMatrixPanel()
{
	for(int i = 0; i < FMB_NUM; ++i)
	{
		delete m_pTB[i];
	}
}

void UI_FaderMatrixPanel::SetBaseWindow(CWnd *pWnd)
{
	GraphicBase::SetBaseWindow(pWnd);

	for(int i = 0; i < FMB_NUM; ++i)
	{
		m_pTB[i]->SetBaseWindow(pWnd);
		m_pTB[i]->SetParent(this);
		m_pTB[i]->SetParentUI(this);
	}
	//m_pRotate->SetBaseWindow(pWnd);
}

void UI_FaderMatrixPanel::SetRect(const CRect *pRect)
{
	GraphicBase::SetRect(pRect);

	int nHeight = (this->m_pRect->Height() - FMB_PADDING * (FMB_NUM - 1)) / 8;

	CRect rc;
	rc.SetRect(this->m_pRect->left, this->m_pRect->top, this->m_pRect->right, this->m_pRect->top + nHeight);
	m_pTB[0]->SetRect(&rc);
	m_pTB[0]->SetColor(COLOR_FORE_MOUSEIN);
	m_pTB[0]->SetBmp(BMP_MIX_EN, &rc);
	
	for(int i = 1; i < FMB_NUM; ++i)
	{
		rc.SetRect(rc.left, rc.top + nHeight + FMB_PADDING, rc.right, rc.bottom + nHeight + FMB_PADDING);
		m_pTB[i]->SetRect(&rc);
		m_pTB[i]->SetColor(COLOR_FORE_MOUSEIN);
		m_pTB[i]->SetBmp(BMP_MIX_EN, &rc);
	}

	//rc.SetRect(this->m_pRect->CenterPoint().x - 27, rc.bottom + 8, this->m_pRect->CenterPoint().x + 27, rc.bottom + 8 + 54);
	//m_pRotate->SetRect(&rc);
}

bool UI_FaderMatrixPanel::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	if(!this->m_pRect->PtInRect(point))
		return false;

	for(int i = 0; i < FMB_NUM; ++i)
	{
		if(m_pTB[i]->OnLButtonDblClk(nFlags, point))
			break;
	}
		
	return true;
}

bool UI_FaderMatrixPanel::OnPaint(CPaintDC *pDC, CDC *pMemDC)
{
#ifdef _DEBUG
	//::GetDC(this->m_pWnd->GetSafeHwnd())
	CPaintDC dc(this->m_pWnd);
	this->SetDC(&dc);
	//this->DrawRect(this->m_pRect, &prop);
#endif

	CRect rc;
	for(int i = 0; i < FMB_NUM; ++i)
	{
		m_pTB[i]->OnPaint(pDC, pMemDC);
	}

	//m_pRotate->OnPaint();
	GraphicBase::OnPaint(pDC, pMemDC);
	return true;
}

bool UI_FaderMatrixPanel::OnLButtonDown(UINT nFlags, CPoint point)
{
	for(int i = 0; i < FMB_NUM; ++i)
	{
		//m_pFMB[i]->OnLButtonDown(nFlags, point);
	}

	GraphicBase::OnLButtonDown(nFlags, point);
	return true;
}

bool UI_FaderMatrixPanel::OnLButtonUp(UINT nFlags, CPoint point)
{
	for(int i = 0; i < FMB_NUM; ++i)
	{
		//m_pFMB[i]->OnLButtonUp(nFlags, point);
	}
	GraphicBase::OnLButtonUp(nFlags, point);
	return true;
}

bool UI_FaderMatrixPanel::OnMouseMove(UINT nFlags, CPoint point)
{
	for(int i = 0; i < FMB_NUM; ++i)
	{
		m_pTB[i]->OnMouseMove(nFlags, point);
	}

	GraphicBase::OnMouseMove(nFlags, point);
	return true;
}

void UI_FaderMatrixPanel::Redraw()
{
#ifdef _DEBUG
	GRAPHICPROP prop;
	//this->DrawRect(this->m_pRect, &prop);
#endif

	for(int i = 0; i < FMB_NUM; ++i)
	{
		m_pTB[i]->Redraw();
	}
	GraphicBase::Redraw();
}

void UI_FaderMatrixPanel::Save(const CString& path)
{
	for(int i = 0; i < FMB_NUM; ++i)
	{
		//m_TB[i].Save(path);
	}
}

void UI_FaderMatrixPanel::Load(const CString& path)
{
	for(int i = 0; i < FMB_NUM; ++i)
	{
		//m_TB[i].Load(path);
	}
}

void UI_FaderMatrixPanel::Func(int param)
{
	if(!this->m_pParent)
		return;

	if(::g_GBList.size() == 0)
		return;

	
	unsigned char *p = (unsigned char *)param;
	int nFlag = (int)*p++;
	int nOut = (int)*p++;
	p++;
	if(*p != '\0')
		return;

	int nIn = this->GetID();

	GraphicBase *pGB[5];
	int i = 0;
	for(std::list<GraphicBase *>::iterator it = ::g_GBList.begin(); 
		it != ::g_GBList.end(); ++it)
	{
		pGB[i++] = *it;
	}

	UI_InputPanel *pIn = (UI_InputPanel *)pGB[0];
	UI_InputPanel *pOut = (UI_InputPanel *)pGB[1];
	pIn->m_VP[nIn].ToggleEx(nFlag);
	pOut->m_VP[nOut].ToggleEx(nFlag);
}

bool UI_FaderMatrixPanel::OnRButtonDown(UINT nFlags, CPoint point)
{
	if(!this->m_pRect->PtInRect(point))
		return false;

	for(int i = 0; i < FMB_NUM; ++i)
	{
		if(m_pTB[i]->OnRButtonDown(nFlags, point))
			break;
	}

	return true;
}

bool UI_FaderMatrixPanel::OnRButtonUp(UINT nFlags, CPoint point)
{
	if(!this->m_pRect->PtInRect(point))
		return false;

	for(int i = 0; i < FMB_NUM; ++i)
	{
		if(m_pTB[i]->OnRButtonUp(nFlags, point))
			break;
	}

	return true;
}